using System;
using System.Collections.Generic;
using System.Linq;
using Daniel.Plugin.Widgets.HelloWorld.Domain;
using Daniel.Plugin.Widgets.HelloWorld.Services;
using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Data;

namespace Nop.Plugin.Shipping.ByWeight.Services
{
    public partial class EmployeesService : IEmployeesService
    {
        #region Constants
        private const string SHIPPINGBYWEIGHT_ALL_KEY = "Nop.Employees.all-{0}-{1}";
        private const string SHIPPINGBYWEIGHT_PATTERN_KEY = "Nop.Employees.";
        #endregion

        #region Fields

        private readonly IRepository<Employees> _sbwRepository;
        private readonly ICacheManager _cacheManager;

        #endregion

        #region Ctor

        public EmployeesService(ICacheManager cacheManager,
            IRepository<Employees> sbwRepository)
        {
            this._cacheManager = cacheManager;
            this._sbwRepository = sbwRepository;
        }

        #endregion

        #region Methods

        
        public virtual IPagedList<Employees> GetAll(int pageIndex = 0, int pageSize = int.MaxValue)
        {
            string key = string.Format(SHIPPINGBYWEIGHT_ALL_KEY, pageIndex, pageSize);
            return _cacheManager.Get(key, () =>
            {
                var query = from sbw in _sbwRepository.Table
                            orderby sbw.EmployeeID
                            select sbw;

                var records = new PagedList<Employees>(query, pageIndex, pageSize);
                return records;
            });
        }
        #endregion
    }
}
